package com.jbit.dm212.service.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jbit.dm212.entity.dto.DmCinemaSeatDTO;
import com.jbit.dm212.entity.dto.DmSchedulerSeatDTO;
import com.jbit.dm212.entity.po.DmCinema;
import com.jbit.dm212.entity.po.DmScheduler;
import com.jbit.dm212.entity.po.DmSchedulerSeat;
import com.jbit.dm212.entity.po.DmSchedulerSeatPrice;
import com.jbit.dm212.entity.vo.DmSchedulerVO;
import com.jbit.dm212.mapper.DmSchedulerMapper;
import com.jbit.dm212.mapper.DmSchedulerSeatMapper;
import com.jbit.dm212.service.DmSchedulerSeatService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class DmSchedulerSeatServiceImpl implements DmSchedulerSeatService {
    @Resource
    private DmSchedulerSeatMapper schedulerSeatMapper;
    @Override
    public DmCinemaSeatDTO getCinemaSeatArray(DmScheduler scheduler) {
        MPJLambdaWrapper<DmSchedulerSeat> lambdaWrapper = new MPJLambdaWrapper<>();

//        lambdaWrapper.select(DmScheduler::getCinemaId);
        lambdaWrapper.groupBy(DmSchedulerSeat::getX);
        lambdaWrapper.orderByAsc(DmSchedulerSeat::getSort);
        lambdaWrapper.eq(DmSchedulerSeat::getScheduleId,scheduler.getId());

        lambdaWrapper.selectAs("GROUP_CONCAT(CASE WHEN areaLevel = 1 THEN 'a' WHEN areaLevel = 2 THEN 'b' WHEN areaLevel = 3 THEN 'c' ELSE '_' END)",DmCinemaSeatDTO::getSeatArray);

        List<Object> list = schedulerSeatMapper.selectObjs(lambdaWrapper);

        List<String> stringList = new ArrayList<>();
        list.forEach(obj->stringList.add(obj.toString()));
        DmCinemaSeatDTO seatDTO = new DmCinemaSeatDTO();
        seatDTO.setCinemaId(scheduler.getCinemaId());
        seatDTO.setSeatArray(stringList);
        return seatDTO;
    }

    @Override
    public List<DmSchedulerSeatDTO> getSeatPrice(List<DmSchedulerSeat> schedulerSeatList) {
        Integer schedulerId = schedulerSeatList.get(0).getScheduleId();

        MPJLambdaWrapper<DmSchedulerSeat> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmSchedulerSeat::getId,DmSchedulerSeat::getAreaLevel,DmSchedulerSeat::getX,DmSchedulerSeat::getY);

        lambdaWrapper.selectSub(DmSchedulerSeatPrice.class,priceLambda->{
            priceLambda.select(DmSchedulerSeatPrice::getPrice);
           priceLambda.eq(DmSchedulerSeatPrice::getAreaLevel,DmSchedulerSeat::getAreaLevel);
            priceLambda.eq(DmSchedulerSeatPrice::getScheduleId,schedulerId);
        },DmSchedulerSeatDTO::getPrice);

        lambdaWrapper.eq(DmSchedulerSeat::getScheduleId,schedulerId);
        lambdaWrapper.and(seatWrapper->{
            for (DmSchedulerSeat schedulerSeat : schedulerSeatList) {
                seatWrapper.or(j->{
                    j.eq(DmSchedulerSeat::getX,schedulerSeat.getX());
                    j.eq(DmSchedulerSeat::getY,schedulerSeat.getY());
                });
            }
        });
        return schedulerSeatMapper.selectJoinList(DmSchedulerSeatDTO.class,lambdaWrapper);
    }

    @Override
    public boolean lockSeat(DmSchedulerSeat schedulerSeat) {
        return schedulerSeatMapper.updateById(schedulerSeat) == 1;
    }

}
